package com.sy.service.purchase.impl;

import com.alibaba.excel.EasyExcel;
import com.sy.bean.purchase.Purchase;
import com.sy.bean.purchase.PurchaseDetail;
import com.sy.bean.purchase.PurchaseExcel;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.mapper.purchase.PurchaseDetailMapper;
import com.sy.mapper.purchase.PurchaseMapper;
import com.sy.service.purchase.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseServiceImpl extends BaseServiceImpl<Purchase, PurchaseMapper> implements PurchaseService {

    String currentMaxCode; //当前最大编码 CG2024001
    @Autowired
    PurchaseDetailMapper purchaseDetailMapper;

    @Autowired
    PurchaseMapper purchaseMapper;
    @Autowired
    HttpServletRequest request;
    @Override
    public String export(Map map) {
        List<PurchaseExcel>list=purchaseMapper.export(map);
        String root=request.getServletContext().getRealPath("/")+"/excel";
        File file=new File(root);
        if (!file.exists())
            file.mkdirs();
        String fileName= file.getAbsolutePath()+"/采购表.xlsx";
        EasyExcel.write(fileName, PurchaseExcel.class)
                .sheet("采购表")
                .doWrite(()->{
                    return list;
                });
        return "/excel/采购单列表.xlsx";
    }
    @Transactional
    @Override
    public int save(Purchase entity) {
        entity.setCode(generateCode());
        //保存采购单
        int rs = super.save(entity);
        if (rs == 0)
            throw new RuntimeException("保存采购单失败");
        //保存采购名细
        rs = purchaseDetailMapper.batchSave(entity.getId(), entity.getPurchaseDetail());
        if (rs == 0)
            throw new RuntimeException("保存采购单明细失败");
        return 1;
    }


     String generateCode() {
        String year = getYear();
        //判断当前最大编码是否存在
        if (null == currentMaxCode) {
            //如果不存在，查询当年最大编码
            currentMaxCode = baseMapper.queryMaxCode(year);
            if (null == currentMaxCode) {
                //说明不存在，生产第一个编码
                currentMaxCode = "CG" + year + "001";
            } else {
                //如果存在，新编码序号+1
                getNextCode();
            }
        } else {
            //如果存在，新编码序号+1
            getNextCode();
        }
        return currentMaxCode;
    }

    void getNextCode() {
        //CG2024001
        String prefix = currentMaxCode.substring(0, 6);
        String seq = currentMaxCode.substring(6);
        //转换数字001. tmp=1
        int tmp = Integer.parseInt(seq);
        tmp++;
        currentMaxCode = prefix + String.format("%03d", tmp);
    }

    String getYear() {
        LocalDate localDate = LocalDate.now();
        return String.valueOf(localDate.getYear());
    }

    @Override
    public int batchDelete(List<Integer> ids) {
        int rs= purchaseMapper.batchDelete(ids);
        if (rs<1)
            throw new RuntimeException("批量删除采购单失败");
         rs=purchaseDetailMapper.queryDetail(ids);
        if (rs>1) {
            rs=purchaseDetailMapper.batchDelete(ids);
            System.out.println(rs);
            if (rs<1)
                throw new RuntimeException("批量删除采购单明细失败");
        }
        return 1;
    }


}
